package tools.timezone;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 */
public class DayLight {

    private TimeZone timeZone;
    private Date startDay = new Date();
    private Date endDay;

    public static DayLight create(TimeZone timeZone) {
        DayLight dayLight = new DayLight();
        dayLight.setTimeZone(timeZone);
        return dayLight;
    }

    public void process(String dayStr) {
        process(dayStr, dayStr);
    }
    public void process(String startDayStr, String endDayStr) {
        parseDateInterval(startDayStr, endDayStr);
    }

    private void parseDateInterval(String startDayStr, String endDayStr) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setTimeZone(timeZone);

        try {
            this.startDay = dateFormat.parse(startDayStr);
            this.endDay = dateFormat.parse(endDayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (this.startDay.equals(this.endDay)) {
            Calendar cal = Calendar.getInstance(timeZone);
            cal.add(Calendar.DATE, 1);
        }
    }

    public List<Date> getHours() {
        List<Date> result = new ArrayList<Date>();
        final Calendar cal = Calendar.getInstance(timeZone);
        cal.setTime(startDay);
        do{
            result.add(cal.getTime());
            cal.add(Calendar.HOUR, 1);
        } while (cal.getTime().getTime() <= endDay.getTime());
        return result;
    }

    private void setTimeZone(TimeZone timeZone) {
        this.timeZone = timeZone;
    }

    private DayLight(){}

    public static void main(String[] args) {
        final DayLight dayLight = DayLight.create(TimeZone.getTimeZone("America/New_York"));
        dayLight.process("2012-11-04");
        final List<Date> hours = dayLight.getHours();
        for (Date hour : hours) {
            System.out.println(hour);
        }
    }
}
